package com.br.aiops.schedule;

import com.br.aiops.cycle.Cycle;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.time.FastDateFormat;

import java.util.Date;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ScheduleCycle {

    private Cycle prevCycle;
    private Cycle currentCycle;

    public ScheduleCycle(int duration) {
        this.currentCycle = new Cycle(duration);

        this.prevCycle = new Cycle();
        this.prevCycle.setEnd(this.currentCycle.getStart());
        this.prevCycle.setStart(this.prevCycle.next(this.prevCycle.getEnd(), -duration));
    }

    public boolean inCurrentCycle(Date date) {
        long minute = Long.parseLong(FastDateFormat.getInstance("yyyyMMddHHmm").format(date));
        return currentCycle.getStart() <= minute && minute < currentCycle.getEnd() ;
    }

    public boolean inPrevCycle(Date date) {
        long minute = Long.parseLong(FastDateFormat.getInstance("yyyyMMddHHmm").format(date));
        return prevCycle.getStart() <= minute && minute < prevCycle.getEnd() ;
    }
}
